package com.atguigu.dao.impl;

import com.atguigu.dao.BookDao;
import com.atguigu.pojo.impl.Book;

import java.util.List;

public class BookDaoImpl extends BaseDao implements BookDao  {
    @Override
    public int addBook(Book book) {
        String sql = "insert into t_book(`name`,`author`,`price`,`sales`,`stock`,`img_path`) values(?,?,?,?,?,?)";
        return update(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImg_path());
    }

    @Override
    public int deleteBookById(Integer id) {
        String sql = "delete from t_book where id=?";
        return update(sql,id);
    }

    @Override
    public int updateBook(Book book) {
        String sql = "UPDATE t_book SET name=?,author=?,price=?,sales=?,stock=?,img_path=? WHERE id=?";
        return update(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImg_path(),book.getId());
    }

    @Override
    public Book queryBookById(Integer id) {
        String sql ="SELECT `id` , `name` , `author` , `price` , `sales` , `stock` , `img_path` FROM t_book WHERE id=?";
        return queryForOne(Book.class,sql,id);
    }

    @Override
    public List<Book> queryBooks() {
        String sql ="SELECT `id` , `name` , `author` , `price` , `sales` , `stock` , `img_path` FROM t_book";
        return querForList(Book.class,sql);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql = "SELECT COUNT(*) FROM t_book";
        Number count = (Number) querForStringValue(sql);
        return  count.intValue();
    }

    @Override
    public List<Book> queruForPageItems(int begin, int pageSize) {
        String sql = "SELECT `id`,`name`,`author`,`price`,`sales`,`stock`,`img_path` FROM t_book LIMIT ?,?";
        return querForList(Book.class,sql,begin,pageSize);
    }

    @Override
    public Integer queryForPageTotalCountByPrice(int min, int max) {
        String sql = "SELECT COUNT(*) FROM t_book where price between ? and ?";
        Number count = (Number) querForStringValue(sql,min,max);
        return  count.intValue();
    }

    @Override
    public List<Book> queruForPageItemsByPrice(int begin, int pageSize, int min, int max) {
        String sql = "SELECT `id`,`name`,`author`,`price`,`sales`,`stock`,`img_path` FROM t_book where price between ? and ? order by price LIMIT ?,?";
        return querForList(Book.class,sql,min,max,begin,pageSize);
    }
}
